package com.dynamixsoftware.printershare.ipp;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class IppConnection
{
  private HttpURLConnection con;
  private String connect_url;
  private String login;
  private String password;
  private String service_url;
  private String user_agent;
  
  public IppConnection(String paramString1, String paramString2)
  {
    int i = paramString1.indexOf("://");
    this.connect_url = ("http://" + paramString1.substring(i + 3));
    this.service_url = paramString1;
    this.user_agent = paramString2;
  }
  
  public void disconnect()
  {
    if (this.con != null) {
      this.con.disconnect();
    }
  }
  
  public IppMessage getResponse()
    throws Exception
  {
    try
    {
      int i = this.con.getResponseCode();
      if (i != 200) {
        throw new Exception("Can't connect to " + this.service_url + ". HTTP error " + i);
      }else {
    	  InputStream localInputStream = this.con.getInputStream();
          IppMessage localIppMessage = new IppMessage();
          localIppMessage.readFromStream(localInputStream);
          return localIppMessage;
      }
    }
    finally
    {
      try
      {
        if (this.con != null) {
          this.con.disconnect();
        }
        this.con = null;
      }
      catch (Exception localException1)
      {
        for (;;)
        {
          try
          {
            InputStream localInputStream;
            if (this.con != null) {
              this.con.disconnect();
            }
            this.con = null;
//            return localIppMessage;
          }
          catch (Exception localException2)
          {
            localException2.printStackTrace();
          }
          localException1 = localException1;
          localException1.printStackTrace();
        }
      }
//      InputStream localInputStream = this.con.getInputStream();
//      IppMessage localIppMessage = new IppMessage();
//      localIppMessage.readFromStream(localInputStream);
//      return localIppMessage;
    }
  }
  
  public String getUri()
  {
    return this.service_url;
  }
  
  public boolean isAuthorization()
  {
    return this.login != null;
  }
  
  public boolean isSecured()
  {
    return this.connect_url.startsWith("https://");
  }
  
  public OutputStream sendRequest(IppMessage paramIppMessage)
    throws Exception
  {
    this.con = ((HttpURLConnection)new URL(this.connect_url).openConnection());
    this.con.setConnectTimeout(15000);
    this.con.setReadTimeout(15000);
    this.con.setDoInput(true);
    this.con.setDoOutput(true);
    this.con.setUseCaches(false);
    this.con.setRequestMethod("POST");
    this.con.setRequestProperty("User-Agent", this.user_agent);
    if (this.login != null) {
      this.con.setRequestProperty("Authorization", "Basic " + Base64.encodeString(new StringBuilder().append(this.login).append(":").append(this.password).toString()));
    }
    this.con.setRequestProperty("Content-Type", "application/ipp");
    this.con.setChunkedStreamingMode(32768);
    OutputStream localOutputStream = this.con.getOutputStream();
    paramIppMessage.writeToStream(localOutputStream);
    return localOutputStream;
  }
  
  public void setAuthorization(String paramString1, String paramString2)
  {
    this.login = paramString1;
    this.password = paramString2;
  }
  
  public void setSecured(boolean paramBoolean)
  {
    int i = this.connect_url.indexOf("://");
    StringBuilder localStringBuilder = new StringBuilder();
    if (paramBoolean) {}
    for (String str = "https";; str = "http")
    {
      this.connect_url = (str + this.connect_url.substring(i));
      return;
    }
  }
}


/* Location:           C:\Users\Admin\Desktop\Decompiler\JAD\jd-gui-0.3.6.windows\classes_dex2jar.jar
 * Qualified Name:     com.dynamixsoftware.printershare.ipp.IppConnection
 * JD-Core Version:    0.7.0.1
 */